package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    private final NursingPlanMapper nursingPlanMapper;

    @Autowired
    public NursingPlanServiceImpl(NursingPlanMapper nursingPlanMapper) {
        this.nursingPlanMapper = nursingPlanMapper;
    }

    @Override
    public List<NursingPlanVo> selectAll() {
        return nursingPlanMapper.selectAll();
    }

    @Override
    @Transactional
    public void addPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(1);
        nursingPlanMapper.addPlan(nursingPlan);
        nursingPlanDto.getProjectPlans().forEach(v -> v.setPlanId(nursingPlan.getId()));
        nursingProjectPlanService.addList(nursingPlanDto.getProjectPlans());
    }

    @Override
    public PageResponse<NursingPlanVo> pageSearch(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<NursingPlan>> lists = nursingPlanMapper.pageSearch(pageNum, pageSize, name, status);
        return PageResponse.of(lists, NursingPlanVo.class);
    }

    @Override
    public NursingPlanVo getPlanById(Long id) {
        return nursingPlanMapper.getPlanById(id);
    }

    @Override
    @Transactional
    public void updatePlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        NursingPlanVo byId = getPlanById(nursingPlanDto.getId());
        List<Long> ids = byId.getProjectPlans().stream().map(NursingProjectPlanVo::getId).collect(Collectors.toList());
        nursingProjectPlanService.deleteByIds(ids);
        if (CollUtil.isNotEmpty(nursingPlanDto.getProjectPlans())) {
            nursingPlanDto.getProjectPlans().forEach(v -> v.setPlanId(nursingPlanDto.getId()));
            nursingProjectPlanService.addList(nursingPlanDto.getProjectPlans());
        }
        nursingPlanMapper.updatePlan(nursingPlan);
    }

    @Override
    public void deletePlan(Long id) {
        nursingProjectPlanService.deletePlanById(id);
        nursingPlanMapper.deletePlan(id);
    }

    @Override
    public void ture_false(Long id, Integer status) {
        nursingPlanMapper.ture_false(id, status);
    }


}
